package com.nnnu.orange.controller.user;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nnnu.orange.constant.JwtClaimsConstant;
import com.nnnu.orange.constant.MessageConstants;
import com.nnnu.orange.pojo.dto.*;
import com.nnnu.orange.pojo.entity.Users;
import com.nnnu.orange.properties.JwtProperties;
import com.nnnu.orange.result.Result;
import com.nnnu.orange.service.IUsersService;
import com.nnnu.orange.utils.JwtUtil;
import com.nnnu.orange.utils.ThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 存储用户的详细信息 前端控制器
 * </p>
 *
 * @author zk
 * @since 2024-11-07
 */
@RestController("userUsersController")
@RequestMapping("/user")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "用户端相关接口")
public class UsersController {

    private final IUsersService usersService;
    private final JwtProperties jwtProperties;


    /**
     * 用户登录
     *
     * @param loginDto dto
     * @return token
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<?> login(@RequestBody UserLoginDto loginDto) {
        log.info("用户登录:{}", loginDto);
        Users user = usersService.login(loginDto);
        //生成token
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
        String token = JwtUtil.createJwt(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        usersService.update(new LambdaUpdateWrapper<Users>()
                .set(Users::getLastLogin, LocalDateTime.now()).eq(Users::getUserId, user.getUserId()));
        return Result.success(token, MessageConstants.LOGIN_SUCCESS);
    }

    /**
     * 用户修改密码
     *
     * @param paDto dto
     * @return 提示
     */
    @PutMapping("/changePa")
    public Result<?> changePassword(@RequestBody @Validated UserChangPaDto paDto) {
        log.info("用户修改密码:{}", paDto);
        usersService.updatePassword(paDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }


    /**
     * 用户修改信息
     *
     * @param userDto dto
     * @return 提示
     */
    @PutMapping("/update")
    @ApiOperation("用户修改信息")
    public Result<?> updateUser(@RequestBody @Validated UserDto userDto) {
        log.info("用户修改信息：{}", userDto);
        usersService.updateUserInfo(userDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 用户注册
     *
     * @param userDto dto
     * @return 提示
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result<?> save(@RequestBody @Validated UserRegisterDto userDto) {
        log.info("用户注册:{}", userDto);
        usersService.register(userDto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }


    /**
     * 获取用户详细信息
     *
     * @return 用户实体
     */
    @GetMapping("/detail")
    @ApiOperation("用户获取详细信息")
    public Result<?> getDetail() {
        log.info("用户获取详细信息");
        return Result.success(usersService.getById(ThreadLocalUtil.getId()), MessageConstants.SELECT_OK);
    }

}
